package com.leo.blog.web.blogadmin;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.leo.blog.annotatiton.AccessLimit;

import com.leo.blog.dto.BlogExecution;
import com.leo.blog.dto.BlogTypeExecution;
import com.leo.blog.dto.BlogerExecution;
import com.leo.blog.dto.ImageHolder;
import com.leo.blog.entity.Blog;
import com.leo.blog.entity.BlogType;
import com.leo.blog.entity.Bloger;
import com.leo.blog.enums.BlogStateEnum;
import com.leo.blog.enums.BlogerStateEnum;
import com.leo.blog.exceptions.BlogOperationException;
import com.leo.blog.service.BlogService;
import com.leo.blog.service.BlogTypeService;
import com.leo.blog.service.BlogerService;
import com.leo.blog.util.CodeUtil;
import com.leo.blog.util.HttpServletRequestUtil;
import com.leo.blog.util.ImageUtil;
import com.leo.blog.util.PathUtil;
import com.leo.blog.util.subStringUtil;

@Controller
@RequestMapping(value = "/blogadmin")
public class BlogManagementController {
	@Autowired
	private BlogService blogService;
	@Autowired
	private BlogTypeService blogTypeService;
	@Autowired
	private BlogerService blogerService;
	
	//登陆验证
	@RequestMapping(value = "/register")
	@ResponseBody
	public Map<String, Object> login(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//检查验证码是否正确
		if(!CodeUtil.checkVerifyCode(request)) {
			modelMap.put("success", false);
			modelMap.put("msg","请输入正确的验证码");
			return modelMap;
		}
		
		//获取前台传过来的值
		String userName = HttpServletRequestUtil.getString(request, "username");
		String password = HttpServletRequestUtil.getString(request, "password");
		
		BlogerExecution be = blogerService.getUserByLoginName(userName);
		if(be.getState() == BlogerStateEnum.SUCCESS.getState()) {
			String psw = password + userName;
			if(DigestUtils.md5DigestAsHex(psw.getBytes()).equals(be.getBloger().getPassword())) {
				//把用户信息存进session
				request.getSession().setAttribute("bloger", be.getBloger());
				
				modelMap.put("success", true);
				//跳转页面
				modelMap.put("redirect", "../blogadmin/adminindex");
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "密码错误");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "帐号不存在");
		}		
		return modelMap;
	}
	
	@RequestMapping(value = "/getbloglistbystatus")
	@ResponseBody
	@AccessLimit(seconds = 1, maxCount = 10)
	public Map<String, Object> getBlogListByStatus(){
		Map<String, Object> modelMap = new HashMap<>();
		BlogExecution be = blogService.getBlogListByStatus();
		if(be.getState() == BlogStateEnum.SUCCESS.getState()) {
			modelMap.put("list", be.getBlogList2());
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询信息失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/getblogbyclick")
	@ResponseBody
	@AccessLimit(seconds = 1, maxCount = 10)
	public Map<String, Object> getBlogByClick(){
		Map<String, Object> modelMap = new HashMap<>();
		BlogExecution be = blogService.getBlogByClick();
		if(be.getState() == BlogStateEnum.SUCCESS.getState()) {
			modelMap.put("list", be.getBlogList2());
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/getbloglistbydate")
	@ResponseBody
	@AccessLimit(seconds = 1, maxCount = 10)
	public Map<String, Object> getBlogListByDate(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int status = HttpServletRequestUtil.getInt(request, "status");
		String startTime = HttpServletRequestUtil.getString(request, "startTime");
		String endTime = HttpServletRequestUtil.getString(request, "endTime");
		
		Blog blogCondition = new Blog();
		if(status > 0) {
			blogCondition.setStatus(status);
		}
		if(startTime != null && startTime != "") {
			blogCondition.setStartTime(startTime);
		}
		if(endTime != null && endTime != "") {
			blogCondition.setEndTime(endTime);
		}
		
		BlogExecution be = blogService.getBlogListByDate(blogCondition);
		if(be.getState() == BlogStateEnum.SUCCESS.getState()) {
			modelMap.put("list", be.getBlogList2());
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/getblogbyalltype")
	@ResponseBody
	public Map<String, Object> getBlogByAllType(){
		Map<String, Object> modelMap = new HashMap<>();
		Map<String, List<Blog>> blogMap = blogService.getBlogByAllType();
		if(blogMap.size() > 0) {
			modelMap.put("blogMap", blogMap);
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询数据失败");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getblogbyid", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getBlogById(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int blogId = HttpServletRequestUtil.getInt(request, "blogId");
		if(blogId > 0 ) {
			//获取文章信息
			Blog blog = blogService.getByBlogId(blogId);
			//获取该文章下的文章类型列表
			List<BlogType> blogTypeList = blogTypeService.getBlogTypeList();
			modelMap.put("blog", blog);
			modelMap.put("blogTypeList", blogTypeList);
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "empty blogId");
		}
		return modelMap;
	}
	@RequestMapping(value = "/getblogbyidpre", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getBlogByIdPre(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		Blog blog = null;
		//获取前台传过来的值
		int blogId = HttpServletRequestUtil.getInt(request, "blogId");
		
		while(blog == null && blogId > 0) {
			//获取文章信息
			blog = blogService.getBlogByIdPreAndNext(blogId);
			blogId--;
		}
		if(blog == null ) {
			modelMap.put("success", false);
			modelMap.put("msg", "empty blogId");
		}else {
			modelMap.put("blog", blog);
			modelMap.put("success", true);
		}
		return modelMap;
	}
	@RequestMapping(value = "/getblogbyidnext", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getBlogByIdNext(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		Blog blog = null;
		//获取前台传过来的值
		int blogId = HttpServletRequestUtil.getInt(request, "blogId");
		//获取最大的id
		int maxId = blogService.getBlogByMaxId();
		
		while(blog == null && blogId <= maxId) {
			//获取文章信息
			blog = blogService.getBlogByIdPreAndNext(blogId);
			blogId++;
		}
		if(blog == null ) {
			modelMap.put("success", false);
			modelMap.put("msg", "empty blogId");
		}else {
			modelMap.put("blog", blog);
			modelMap.put("success", true);
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getbloglistbypage")
	@ResponseBody
	@AccessLimit(seconds = 1, maxCount = 15)
	public Map<String, Object> getBlogListByPage(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取前台传过来的每页要求返回的文章数上限
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		/**
		 * 手动放入博主id,后期可以session
		 */
		Bloger bloger = new Bloger();
		bloger.setBlogerId(1);
		int blogerId = bloger.getBlogerId();
		
		//空值判断
		if((pageIndex > -1) && (pageSize > -1) && blogerId > 0) {
			// 获取传入的需要检索的条件
			// 筛选的条件可以进行排列组合
			//获取文章类型
			int blogTypeId = HttpServletRequestUtil.getInt(request, "blogTypeId");
			//文章标题
			String blogTitle = HttpServletRequestUtil.getString(request, "blogTitle");
			//关键词
			String keyWord = HttpServletRequestUtil.getString(request, "keyWord");
			//置顶
			int isTop = HttpServletRequestUtil.getInt(request, "isTop");
			//推荐
			int isRecommend = HttpServletRequestUtil.getInt(request, "isRecommend");
			//状态
			int status = HttpServletRequestUtil.getInt(request, "status");
			//获取组合之后的查询条件
			Blog blogCondition = compactBlogCondition6Search(blogerId,blogTypeId,blogTitle,keyWord,isTop,isRecommend,status);
			//根据查询条件和分页信息获取文章列表,并返回总数
			
			BlogExecution be = blogService.getBlogList(blogCondition, pageIndex, pageSize);
			
			//返回页数
			int page = 0;
			int pageCount = be.getCount();
			if(pageCount > pageSize) {
				if(pageCount%pageSize > 0) {
					page = pageCount/pageSize + 1;
				}else {
					page = pageCount/pageSize;
				}
				
			}else {
				page = 1;
			}
			
			modelMap.put("blogList", be.getBlogList());
			modelMap.put("count", be.getCount());
			modelMap.put("page", page);
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty pageSize or pageIndex");
		}
		return modelMap;
	}
	@RequestMapping(value = "/getbloglistbypageclick")
	@ResponseBody
	@AccessLimit(seconds = 1, maxCount = 15)
	public Map<String, Object> getBlogListByPageClick(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取前台传过来的每页要求返回的文章数上限
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		/**
		 * 手动放入博主id,后期可以session
		 */
		Bloger bloger = new Bloger();
		bloger.setBlogerId(1);
		int blogerId = bloger.getBlogerId();
		
		//空值判断
		if((pageIndex > -1) && (pageSize > -1) && blogerId > 0) {
			// 获取传入的需要检索的条件
			// 筛选的条件可以进行排列组合
			//获取文章类型
			int blogTypeId = HttpServletRequestUtil.getInt(request, "blogTypeId");
			//文章标题
			String blogTitle = HttpServletRequestUtil.getString(request, "blogTitle");
			//关键词
			String keyWord = HttpServletRequestUtil.getString(request, "keyWord");
			//置顶
			int isTop = HttpServletRequestUtil.getInt(request, "isTop");
			//推荐
			int isRecommend = HttpServletRequestUtil.getInt(request, "isRecommend");
			//状态
			int status = HttpServletRequestUtil.getInt(request, "status");
			//获取组合之后的查询条件
			Blog blogCondition = compactBlogCondition6Search(blogerId,blogTypeId,blogTitle,keyWord,isTop,isRecommend,status);
			//根据查询条件和分页信息获取文章列表,并返回总数
			
			BlogExecution be = blogService.getBlogListClick(blogCondition, pageIndex, pageSize);
			modelMap.put("blogList", be.getBlogList());
			modelMap.put("count", be.getCount());
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty pageSize or pageIndex");
		}
		return modelMap;
	}
	
	/**
	 * 组合查询条件，并将条件封装到blogCondition对象里返回
	 * @param blogerId
	 * @param blogTypeId
	 * @param blogTitle
	 * @param keyWord
	 * @param isTop
	 * @param isRecommend
	 * @param status
	 * @return
	 */
	private Blog compactBlogCondition6Search(int blogerId, int blogTypeId, String blogTitle, String keyWord, int isTop,
			int isRecommend, int status) {
		Blog blogCondition = new Blog();
		if(blogerId != 0) {
			//查询该博主下的文章
			Bloger bloger = new Bloger();
			bloger.setBlogerId(blogerId);
			blogCondition.setBloger(bloger);
		}
		if(blogTypeId != 0) {
			//类型查询
			BlogType blogType =new BlogType();
			blogType.setBlogTypeId(blogTypeId);
			blogCondition.setBlogType(blogType);
		}
		if(blogTitle != null) {
			//标题查询
			blogCondition.setBlogTitle(blogTitle);
		}
		if(keyWord != null) {
			//关键词查询
			blogCondition.setKeyWord(keyWord);
		}
		if(isTop != 0) {
			//置顶查询
			blogCondition.setIsTop(isTop);
		}
		if(isRecommend != 0) {
			//推荐查询
			blogCondition.setIsRecommend(isRecommend);
		}
		if(status != 0) {
			// 状态查询
			blogCondition.setStatus(status);
		}
		return blogCondition;
	}

	@RequestMapping(value = "/uploadblogimages", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> uploadBlogImages(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//处理图片上传
		//CommonsMultipartResolver请求处理文件上传
		CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		//isMultipart判断是否包含文件
		if(commonsMultipartResolver.isMultipart(request)) {
			//请求数据包含文件,MultipartResolver的resolveMultipart()方法对请求的数据进行解析
			//然后将文件数据解析成MultipartFile并封装在MultipartHttpServletRequest对象中,最后传给Controller
			MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
			//blogImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("blogImg");
			Iterator<String> fileNamesIter = multipartHttpServletRequest.getFileNames();
			while(fileNamesIter.hasNext()) {
				MultipartFile file = multipartHttpServletRequest.getFile(fileNamesIter.next());
				if(file != null) {
					String myFileName = file.getOriginalFilename();
					if(myFileName.trim() != "") {
						String fileName = file.getOriginalFilename();
						//保存图片在本地
						/* 
						 * 先手动赋值博主的id,后面直接用session获取
						 */
						Bloger bloger = (Bloger)request.getSession().getAttribute("bloger");
						int blogerId = bloger.getBlogerId();
						try {
							ImageHolder imageHolder = new ImageHolder(fileName, file.getInputStream());
							String fileNames = blogService.uploadBlogImages("上传了图片",blogerId, imageHolder);
									
							if(fileNames != null) {
								//true
								modelMap.put("success", true);
								modelMap.put("path", fileNames);
							}else {
								modelMap.put("success", false);
							}
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "上传图片不能为空");
			return modelMap;
		}
		return modelMap;
	}
	
	//删除图片
	@RequestMapping(value = "/deletepic")
	@ResponseBody
	public Map<String, Object> deletePic(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//前台获取的值
		String path = HttpServletRequestUtil.getString(request, "path");
		
		Bloger bloger = (Bloger) request.getSession().getAttribute("bloger");
		if(bloger.getHasPermission() == 1) {
			String tempPath = path.replace("/", "\\");
			File fileTemp = new File("F:" + tempPath);
			if(fileTemp.exists()) {
				if(fileTemp.isFile()) {
					if(fileTemp.delete()) {
						modelMap.put("success", true);
						modelMap.put("msg", "删除成功");
					}else {
						modelMap.put("success", false);
						modelMap.put("msg", "删除失败");
					}
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "文件错误");
				}
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "文件不存在");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/modifyblog", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifyBlog(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		Blog blog = new Blog();
		//获取前端取到的值
		int blogId = HttpServletRequestUtil.getInt(request, "blogId");
		
		int status = HttpServletRequestUtil.getInt(request, "status");
		int blogTypeId = HttpServletRequestUtil.getInt(request, "blogTypeId");
		String blogTitle = HttpServletRequestUtil.getString(request, "blogTitle");
		String blogIntroduction = HttpServletRequestUtil.getString(request, "blogIntroduction");
		String keyWord = HttpServletRequestUtil.getString(request, "keyWord");
		String blogImage = HttpServletRequestUtil.getString(request, "blogImage");
		String blogContent = HttpServletRequestUtil.getString(request, "blogContent");
		int isTop = HttpServletRequestUtil.getInt(request, "isTop");
		int isRecommend = HttpServletRequestUtil.getInt(request, "isRecommend");
		int clickNum = HttpServletRequestUtil.getInt(request, "clickNum");
		int commentNum = HttpServletRequestUtil.getInt(request, "commentNum");
		int agreeNum = HttpServletRequestUtil.getInt(request, "agreeNum");
		
		
		/**
		 * 手动设置blogerId,后期session
		 */
		Bloger bloger = (Bloger) request.getSession().getAttribute("bloger");
		//bloger.setBlogerId(1);
		//bloger.setHasPermission(1);
		
		if(bloger.getHasPermission() == 1) {
			//放入blog
			blog.setBlogId(blogId);
			
			//判断空值
			if(status != 0) {
				blog.setStatus(status);
			}
			if(blogTypeId != 0) {
				BlogType blogType = new BlogType();
				blogType.setBlogTypeId(blogTypeId);
				blog.setBlogType(blogType);
			}
			if(blogTitle != null && blogTitle != "") {
				blog.setBlogTitle(blogTitle);
			}
			if(blogIntroduction != null && blogIntroduction != "") {
				blog.setBlogIntroduction(blogIntroduction);
			}
			//将中文的分号转换成英文的分号
			if(keyWord != null && keyWord != "") {
				blog.setKeyWord(subStringUtil.subKeyword(keyWord));
			}
			//图片处理
			if(blogImage != null && blogImage != "") {
				String addr = subStringUtil.subImageInsert(blogImage);
				String imageAddr = "/" + addr;
				blog.setBlogImage(imageAddr);
			}
			if(blogContent != null && blogContent != "") {
				blog.setBlogContent(blogContent);
			}
			if(isTop != 0) {
				blog.setIsTop(isTop);
			}
			if(isRecommend != 0) {
				blog.setIsRecommend(isRecommend);
			}
			if(clickNum != 0) {
				blog.setClickNum(clickNum);
			}
			if(commentNum != 0) {
				blog.setCommentNum(commentNum);
			}
			if(agreeNum != 0) {
				blog.setAgreeNum(agreeNum);
			}
			
			try {
				//执行更新操作
				BlogExecution be = blogService.modifyBlog("更新博客", blog);
				if(be.getState() == BlogStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
					modelMap.put("msg", "更新成功");
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "更新失败");
				}
			} catch (RuntimeException e) {
				modelMap.put("success", false);
				modelMap.put("msg", e.toString());
				return modelMap;
			}	
			
			
			
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/modifyblogclicknum", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifyBlogClickNum(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		Blog blog = new Blog();
		//获取前端取到的值
		int blogId = HttpServletRequestUtil.getInt(request, "blogId");
		
		int status = HttpServletRequestUtil.getInt(request, "status");
		int blogTypeId = HttpServletRequestUtil.getInt(request, "blogTypeId");
		String blogTitle = HttpServletRequestUtil.getString(request, "blogTitle");
		String blogIntroduction = HttpServletRequestUtil.getString(request, "blogIntroduction");
		String keyWord = HttpServletRequestUtil.getString(request, "keyWord");
		String blogImage = HttpServletRequestUtil.getString(request, "blogImage");
		String blogContent = HttpServletRequestUtil.getString(request, "blogContent");
		int isTop = HttpServletRequestUtil.getInt(request, "isTop");
		int isRecommend = HttpServletRequestUtil.getInt(request, "isRecommend");
		int clickNum = HttpServletRequestUtil.getInt(request, "clickNum");
		int commentNum = HttpServletRequestUtil.getInt(request, "commentNum");
		int agreeNum = HttpServletRequestUtil.getInt(request, "agreeNum");
		
		
		
		//放入blog
		blog.setBlogId(blogId);
		
		//判断空值
		if(status != 0) {
			blog.setStatus(status);
		}
		if(blogTypeId != 0) {
			BlogType blogType = new BlogType();
			blogType.setBlogTypeId(blogTypeId);
			blog.setBlogType(blogType);
		}
		if(blogTitle != null && blogTitle != "") {
			blog.setBlogTitle(blogTitle);
		}
		if(blogIntroduction != null && blogIntroduction != "") {
			blog.setBlogIntroduction(blogIntroduction);
		}
		//将中文的分号转换成英文的分号
		if(keyWord != null && keyWord != "") {
			blog.setKeyWord(subStringUtil.subKeyword(keyWord));
		}
		//图片处理
		if(blogImage != null && blogImage != "") {
			String addr = subStringUtil.subImageInsert(blogImage);
			String imageAddr = "/" + addr;
			blog.setBlogImage(imageAddr);
		}
		if(blogContent != null && blogContent != "") {
			blog.setBlogContent(blogContent);
		}
		if(isTop != 0) {
			blog.setIsTop(isTop);
		}
		if(isRecommend != 0) {
			blog.setIsRecommend(isRecommend);
		}
		if(clickNum != 0) {
			blog.setClickNum(clickNum);
		}
		if(commentNum != 0) {
			blog.setCommentNum(commentNum);
		}
		if(agreeNum != 0) {
			blog.setAgreeNum(agreeNum);
		}
			
		try {
			//执行更新操作
			BlogExecution be = blogService.modifyBlogClickNum("更新点击数", blog);
			if(be.getState() == BlogStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
				modelMap.put("msg", "更新成功");
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "更新失败");
			}
		} catch (RuntimeException e) {
			modelMap.put("success", false);
			modelMap.put("msg", e.toString());
			return modelMap;
		}	
		
	return modelMap;
	}
	
	@RequestMapping(value = "/addblog",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addBlog(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		
		//1.接收并转化相应的参数,包括店铺信息以及图片信息, 加一个方法类,作用转化成相应的数据类型
		String blogStr = HttpServletRequestUtil.getString(request, "blogStr");
		//ObjectMapper可以方便的将模型对象转换为JSON，或者JSON生成相应的模型类
		ObjectMapper mapper = new ObjectMapper();
		Blog blog = null;
		try {
			//readValue()====json转pojo,即将json转成对象
			blog = mapper.readValue(blogStr, Blog.class);
		} catch (Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.getMessage());
			return modelMap;
		}
		
		/* 
		 * 先手动赋值博主的id,后面直接用session获取
		 */
		Bloger bloger = (Bloger)request.getSession().getAttribute("bloger");
		//bloger.setBlogerId(1);
		blog.setBloger(bloger);
		if(bloger.getHasPermission() == 1) {
			//添加/发表文章
			if(blog !=null) {		
				//将中文的分号转换成英文的分号
				if(blog.getKeyWord() != null && blog.getKeyWord() != "") {
					blog.setKeyWord(subStringUtil.subKeyword(blog.getKeyWord()));
				}
				//处理图片路径
				if(blog.getBlogImage() != null) {
					String addr = subStringUtil.subImageInsert(blog.getBlogImage());
					String imageAddr = "/" + addr;
					blog.setBlogImage(imageAddr);
				}
				
				BlogExecution be;
				try {
					be = blogService.addBlog("发表博客", blog);//添加/发表文章
					//判断添加的状态返回值是否成功,成功操作
					if(be.getState() == BlogStateEnum.SUCCESS.getState()) {
						//添加的文章类型数量
						int blogTypeId = blog.getBlogType().getBlogTypeId();
						BlogTypeExecution blogType = blogTypeService.getBlogTypeById(blogTypeId);
						int num = blogType.getBlogType().getBlogTypeNum();
						num++;
						blogType.getBlogType().setBlogTypeNum(num);
						blogTypeService.editBlogType("更新了博客类型", blogType.getBlogType());
						
						modelMap.put("success", true);
					}else {
						modelMap.put("success", false);
						modelMap.put("errMsg", be.getStateInfo());
					}
				} catch (BlogOperationException e) {
					modelMap.put("success", false);
					modelMap.put("errMsg", e.getMessage());
					return modelMap;
				}
			}else {
				modelMap.put("success", false);
				modelMap.put("errMsg", "请输入文章信息");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "无权限操作");
		}		
		return modelMap;
	}
	
	@RequestMapping(value = "/getblogcountbystatus")
	@ResponseBody
	public Map<String, Object> getBlogCountByStatus(){
		Map<String, Object> modelMap = new HashMap<>();
		List<Blog> blogCountByStatusList = new ArrayList<>();
		try {
			blogCountByStatusList = blogService.getBlogCountByStatus();
			modelMap.put("blogCountByStatusList", blogCountByStatusList);
			modelMap.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("error", e.getMessage());
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getblogtypelist")
	@ResponseBody
	public Map<String, Object> getBlogTypeList(){
		Map<String, Object> modelMap = new HashMap<>();
		List<BlogType> blogTypeList = new ArrayList<>();
		try {
			blogTypeList = blogTypeService.getBlogTypeList();
			modelMap.put("blogTypeList", blogTypeList);
			modelMap.put("success", true);
		}catch (Exception e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("error", e.getMessage());
		}
		return modelMap;
	}
}
